package com.enzoism.java;

import java.util.concurrent.CountDownLatch;

/**
 * Created by enzoism on 2019/6/9.
 */
public abstract class BaseHealthChecker implements Runnable {

    private CountDownLatch _latch;      // 定时器
    private String _serviceName;        // 服务的名称
    private boolean _serviceUp;         // 判断服务有没有正常启动

    public BaseHealthChecker(String serviceName, CountDownLatch latch)
    {
        super();
        this._latch = latch;
        this._serviceName = serviceName;
        this._serviceUp = false;
    }

    @Override
    public void run() {
        try {
            verifyService();
            _serviceUp = true;
        } catch (Throwable t) {
            t.printStackTrace(System.err);
            _serviceUp = false;
        } finally {
            if(_latch != null) {
                _latch.countDown();
            }
        }
    }

    public String getServiceName() {
        return _serviceName;
    }

    public boolean isServiceUp() {
        return _serviceUp;
    }
    // 当前类为BaseHealthCheck，让不同的子类去分别实现
    public abstract void verifyService();
}
